/*
 * Finals Calculator
 *
 * This file is part of Finals Calculator.
 * Finals Calculator is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Finals Calculator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with FinalsCalculator; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * <p>Calculates what someone need on their final exam given their current grade.</p>
 *
 * @author Cauchy Choi
 * @author Jonathan Chang
 */
public class Calculator
{
	/** The <code>GradingScaleWindow</code> with the grading scale. */
	private GradingScaleWindow myWindow;
	
	
	/**
	 * Creates new Calculator object.
	 */
	public Calculator()
	{
		
	}
	
	
	/**
	 * Creates new Calculator object.
	 *
	 * @param window The GradingScaleWindow with the grading scale
	 */
	public Calculator(GradingScaleWindow window)
	{
		myWindow = window;
	}
	
	
	/**
	 * Calculate grade with weighted course formula with a standard grading scale.
	 * 
	 * @param currentGrade The current grade the user has in the course
	 * @param finalWorth The percentage of the course that the final exam is worth
	 * @param desiredGrade The overall letter grade the user wants to have
	 * @param round Whether or not the user's teacher rounds the overall grade in the course
	 * @return Percent grade of what the user needs on the final exam.
	 */
	public double weightedStandardGrading(double currentGrade, double finalWorth, String desiredGrade, String round)
	{
		//Find desired percent in the course given the desired letter grade.
		double desiredGradeNumber = 0.0;
		if (round.equalsIgnoreCase("y"))
		{
			if (desiredGrade.equalsIgnoreCase("A+") || desiredGrade.equalsIgnoreCase("A") || desiredGrade.equalsIgnoreCase("A-"))
				desiredGradeNumber = 0.895;
			else if (desiredGrade.equalsIgnoreCase("B+") || desiredGrade.equalsIgnoreCase("B") || desiredGrade.equalsIgnoreCase("B-"))
				desiredGradeNumber = 0.795;
			else if (desiredGrade.equalsIgnoreCase("C+") || desiredGrade.equalsIgnoreCase("C") || desiredGrade.equalsIgnoreCase("C-"))
				desiredGradeNumber = 0.695;
			else if (desiredGrade.equalsIgnoreCase("D+") || desiredGrade.equalsIgnoreCase("D") || desiredGrade.equalsIgnoreCase("D-"))
				desiredGradeNumber = 0.595;
			else if (desiredGrade.equalsIgnoreCase("F"))
				desiredGradeNumber = 0.0;
		}
		else if (round.equalsIgnoreCase("n"))
		{
			if (desiredGrade.equalsIgnoreCase("A"))
				desiredGradeNumber = 0.9;
			else if (desiredGrade.equalsIgnoreCase("B"))
				desiredGradeNumber = 0.8;
			else if (desiredGrade.equalsIgnoreCase("C"))
				desiredGradeNumber = 0.7;
			else if (desiredGrade.equalsIgnoreCase("D"))
				desiredGradeNumber = 0.6;
			else if (desiredGrade.equalsIgnoreCase("F"))
				desiredGradeNumber = 0.0;
		}
		
		//Use weighted course formula to calculate final exam grade needed.
		double finalGrade =  ((desiredGradeNumber - ((1 - finalWorth) * currentGrade)) / finalWorth) * 100.0;
		
		return Math.round(finalGrade * 100.0) / 100.0;
	}
	
	
	/**
	 * Calculate grade with non-weighted course formula with a standard grading scale.
	 * 
	 * @param currentGrade The current number of points the user has in the course
	 * @param coursePoints The number of points the course is out of
	 * @param finalWorth The number of points the final exam is worth
	 * @param desiredGrade The overall letter grade the user wants to have
	 * @param round Whether or not the user's teacher rounds the overall grade in the course
	 * @return Percent grade of what the user needs on the final exam.
	 */
	public double noWeightStandardGrading(double currentGrade, double coursePoints, double finalWorth, String desiredGrade, String round)
	{
		//Find desired percent in the course given the desired letter grade.
		double desiredGradeNumber = 0.0;
		if (round.equalsIgnoreCase("y"))
		{
			if (desiredGrade.equalsIgnoreCase("A+") || desiredGrade.equalsIgnoreCase("A") || desiredGrade.equalsIgnoreCase("A-"))
				desiredGradeNumber = 0.895;
			else if (desiredGrade.equalsIgnoreCase("B+") || desiredGrade.equalsIgnoreCase("B") || desiredGrade.equalsIgnoreCase("B-"))
				desiredGradeNumber = 0.795;
			else if (desiredGrade.equalsIgnoreCase("C+") || desiredGrade.equalsIgnoreCase("C") || desiredGrade.equalsIgnoreCase("C-"))
				desiredGradeNumber = 0.695;
			else if (desiredGrade.equalsIgnoreCase("D+") || desiredGrade.equalsIgnoreCase("D") || desiredGrade.equalsIgnoreCase("D-"))
				desiredGradeNumber = 0.595;
			else if (desiredGrade.equalsIgnoreCase("F"))
				desiredGradeNumber = 0.0;
		}
		else if (round.equalsIgnoreCase("n"))
		{
			if (desiredGrade.equalsIgnoreCase("A+") || desiredGrade.equalsIgnoreCase("A") || desiredGrade.equalsIgnoreCase("A-"))
				desiredGradeNumber = 0.9;
			else if (desiredGrade.equalsIgnoreCase("B+") || desiredGrade.equalsIgnoreCase("B") || desiredGrade.equalsIgnoreCase("B-"))
				desiredGradeNumber = 0.8;
			else if (desiredGrade.equalsIgnoreCase("C+") || desiredGrade.equalsIgnoreCase("C") || desiredGrade.equalsIgnoreCase("C-"))
				desiredGradeNumber = 0.7;
			else if (desiredGrade.equalsIgnoreCase("D+") || desiredGrade.equalsIgnoreCase("D") || desiredGrade.equalsIgnoreCase("D-"))
				desiredGradeNumber = 0.6;
			else if (desiredGrade.equalsIgnoreCase("F"))
				desiredGradeNumber = 0.0;
		}
		
		//Use non-weighted course formula to calculate final exam grade needed.
		double finalGrade = (((desiredGradeNumber * (coursePoints + finalWorth)) - currentGrade) / finalWorth) * 100.0;
		
		return Math.round(finalGrade * 100.0) / 100.0;
	}
	
	/**
	 * Calculate grade with weighted course formula with a custom grading scale.
	 * 
	 * @param currentGrade The current grade the user has in the course
	 * @param finalWorth The percentage of the course that the final exam is worth
	 * @param desiredGrade The overall letter grade the user wants to have
	 * @param round Whether or not the user's teacher rounds the overall grade in the course
	 * @return Percent grade of what the user needs on the final exam.
	 */
	public double weightedCustomGrading(double currentGrade, double finalWorth, String desiredGrade, String round)
	{
		//Get grading scale from the GradingScaleWindow.
		double[] scale = myWindow.getScale();
		
		//Find desired percent in the course given the desired letter grade.
		double desiredGradeNumber = 0.0;
		if (round.equalsIgnoreCase("y"))
		{
			if (desiredGrade.equalsIgnoreCase("A+"))
				desiredGradeNumber = scale[0] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("A"))
				desiredGradeNumber = scale[1] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("A-"))
				desiredGradeNumber = scale[2] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("B+"))
				desiredGradeNumber = scale[3] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("B"))
				desiredGradeNumber = scale[4] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("B-"))
				desiredGradeNumber = scale[5] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("C+"))
				desiredGradeNumber = scale[6] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("C"))
				desiredGradeNumber = scale[7] - 0.005;
			else if(desiredGrade.equalsIgnoreCase("C-"))
				desiredGradeNumber = scale[8] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("D+"))
				desiredGradeNumber = scale[9] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("D"))
				desiredGradeNumber = scale[10] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("D-"))
				desiredGradeNumber = scale[11] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("F"))
				desiredGradeNumber = 0.0;
		}
		else if (round.equalsIgnoreCase("n"))
		{
			if (desiredGrade.equalsIgnoreCase("A+"))
				desiredGradeNumber = scale[0];
			else if (desiredGrade.equalsIgnoreCase("A"))
				desiredGradeNumber = scale[1];
			else if (desiredGrade.equalsIgnoreCase("A-"))
				desiredGradeNumber = scale[2];
			else if (desiredGrade.equalsIgnoreCase("B+"))
				desiredGradeNumber = scale[3];
			else if (desiredGrade.equalsIgnoreCase("B"))
				desiredGradeNumber = scale[4];
			else if (desiredGrade.equalsIgnoreCase("B-"))
				desiredGradeNumber = scale[5];
			else if (desiredGrade.equalsIgnoreCase("C+"))
				desiredGradeNumber = scale[6];
			else if (desiredGrade.equalsIgnoreCase("C"))
				desiredGradeNumber = scale[7];
			else if(desiredGrade.equalsIgnoreCase("C-"))
				desiredGradeNumber = scale[8];
			else if (desiredGrade.equalsIgnoreCase("D+"))
				desiredGradeNumber = scale[9];
			else if (desiredGrade.equalsIgnoreCase("D"))
				desiredGradeNumber = scale[10];
			else if (desiredGrade.equalsIgnoreCase("D-"))
				desiredGradeNumber = scale[11];
			else if (desiredGrade.equalsIgnoreCase("F"))
				desiredGradeNumber = 0.0;
		}
		
		//Use weighted course formula to calculate final exam grade needed.
		double finalGrade =  ((desiredGradeNumber - ((1 - finalWorth) * currentGrade)) / finalWorth) * 100.0;
		
		return Math.round(finalGrade * 100.0) / 100.0;
	}
	
	
	/**
	 * Calculate grade with non-weighted course formula with a custom grading scale.
	 * 
	 * @param currentGrade The current number of points the user has in the course
	 * @param coursePoints The number of points the course is out of
	 * @param finalWorth The number of points the final exam is worth
	 * @param desiredGrade The overall letter grade the user wants to have
	 * @param round Whether or not the user's teacher rounds the overall grade in the course
	 * @return Percent grade of what the user needs on the final exam.
	 */
	public double noWeightCustomGrading(double currentGrade, double coursePoints, double finalWorth, String desiredGrade, String round)
	{
		//Get grading scale from the GradingScaleWindow.
		double[] scale = myWindow.getScale();
		
		//Find desired percent in the course given the desired letter grade.
		double desiredGradeNumber = 0.0;
		if (round.equalsIgnoreCase("y"))
		{
			if (desiredGrade.equalsIgnoreCase("A+"))
				desiredGradeNumber = scale[0] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("A"))
				desiredGradeNumber = scale[1] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("A-"))
				desiredGradeNumber = scale[2] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("B+"))
				desiredGradeNumber = scale[3] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("B"))
				desiredGradeNumber = scale[4] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("B-"))
				desiredGradeNumber = scale[5] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("C+"))
				desiredGradeNumber = scale[6] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("C"))
				desiredGradeNumber = scale[7] - 0.005;
			else if(desiredGrade.equalsIgnoreCase("C-"))
				desiredGradeNumber = scale[8] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("D+"))
				desiredGradeNumber = scale[9] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("D"))
				desiredGradeNumber = scale[10] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("D-"))
				desiredGradeNumber = scale[11] - 0.005;
			else if (desiredGrade.equalsIgnoreCase("F"))
				desiredGradeNumber = 0.0;
		}
		else if (round.equalsIgnoreCase("n"))
		{
			if (desiredGrade.equalsIgnoreCase("A+"))
				desiredGradeNumber = scale[0];
			else if (desiredGrade.equalsIgnoreCase("A"))
				desiredGradeNumber = scale[1];
			else if (desiredGrade.equalsIgnoreCase("A-"))
				desiredGradeNumber = scale[2];
			else if (desiredGrade.equalsIgnoreCase("B+"))
				desiredGradeNumber = scale[3];
			else if (desiredGrade.equalsIgnoreCase("B"))
				desiredGradeNumber = scale[4];
			else if (desiredGrade.equalsIgnoreCase("B-"))
				desiredGradeNumber = scale[5];
			else if (desiredGrade.equalsIgnoreCase("C+"))
				desiredGradeNumber = scale[6];
			else if (desiredGrade.equalsIgnoreCase("C"))
				desiredGradeNumber = scale[7];
			else if(desiredGrade.equalsIgnoreCase("C-"))
				desiredGradeNumber = scale[8];
			else if (desiredGrade.equalsIgnoreCase("D+"))
				desiredGradeNumber = scale[9];
			else if (desiredGrade.equalsIgnoreCase("D"))
				desiredGradeNumber = scale[10];
			else if (desiredGrade.equalsIgnoreCase("D-"))
				desiredGradeNumber = scale[11];
			else if (desiredGrade.equalsIgnoreCase("F"))
				desiredGradeNumber = 0.0;
		}
		
		//Use non-weighted course formula to calculate final exam grade needed.
		double finalGrade = (((desiredGradeNumber * (coursePoints + finalWorth)) - currentGrade) / finalWorth) * 100.0;
		
		return Math.round(finalGrade * 100.0) / 100.0;
	}
}